﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Drawing;
using System.Xml.Linq;

namespace ProjectBot.Managers
{
    public class GitHubProject
    {
        public XmlDocument Doc { get; private set; }
        public string Author { get; private set; }
        public string ProjectName { get; private set; }
        public string RssLink { get; private set; }
        public string UserLink { get; private set; }
        public string ID { get; private set; }
        public string Description { get; private set; }
        public string Updated { get; private set; }
        public bool Created = false;
        public List<string> FilesModified { get; set; }
        public XmlNode Entry { get; set; }

        private RssFeedReader reader = new RssFeedReader();

        public GitHubProject(string userName, string projectName)
        {
            XmlDocument doc = null;
            System.Threading.Thread thread = new System.Threading.Thread(delegate()
                {
                    reader.TryRead(GitHubManager.ConvertUserAndProject(userName, projectName), out doc);
                    if (doc == null) { ProjectBotForm.WriteLine("Error getting xml", Color.Red); }
                    else
                    {
                        Doc = doc;
                        try
                        {
                            XmlElement root = Doc.DocumentElement;

                            XmlNodeList entrys = root.GetElementsByTagName("entry");
                            // I just want the first entry
                            Entry = entrys[0];
                            Populate();
                        }
                        catch { }
                    }
                });
            thread.IsBackground = true;
            thread.Start();
        }
        public GitHubProject(XmlDocument doc)
        {
            Doc = doc;
            System.Threading.Thread thread = new System.Threading.Thread(delegate()
                {
                    try
                    {
                        XmlElement root = Doc.DocumentElement;

                        XmlNodeList entrys = root.GetElementsByTagName("entry");
                        // I just want the first entry
                        Entry = entrys[0];
                        Populate();
                    }
                    catch { }
                });
            thread.IsBackground = true;
            thread.Start();
        }
        public bool Populate()
        {
            if (Entry == null) { return false; }
            try
            {
                ID = Entry["id"].InnerText;
                Description = Entry["title"].InnerText;
                Updated = Entry["updated"].InnerText;
                XmlNode author = Entry["author"];
                Author = author["name"].InnerText;
                UserLink = author["uri"].InnerText;
                RssLink = Entry["link"].GetAttribute("href");
                // content contains all our files that we touched
                //ProjectBotForm.WriteLine(Entry["content"].InnerText);
                return true;
            }
            catch { }
            return false;
        }
        public XDocument ToXDocument(XmlDocument xmlDocument)
        {
            using (var nodeReader = new XmlNodeReader(xmlDocument))
            {
                nodeReader.MoveToContent();
                return XDocument.Load(nodeReader);
            }
        }
    }
}
